package org.bookie.web.pages;

import org.apache.tapestry.IExternalPage;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.annotations.InjectObject;
import org.apache.tapestry.annotations.InjectPage;
import org.apache.tapestry.engine.IEngineService;
import org.apache.tapestry.engine.ILink;
import org.bookie.framework.services.ChartGenerator.Chart;
import org.bookie.model.User;
import org.bookie.model.services.UserDAO;
import org.bookie.web.framework.CommonExternalPage;

/**
 * Page displaying {@link User} details like username, balance, groups etc.
 * 
 * @author Igor Drobiazko
 *
 */
public abstract class UserDetails extends CommonExternalPage implements IExternalPage{
	
	/**
	 * {@inheritDoc}
	 */
	public void activateExternalPage(Object[] parameters, IRequestCycle cycle) {
		Long id = null;
		if (0<parameters.length) {
			id = (Long) parameters[0];
		}
		if (null == id) {
			recordError("userdetails.id.missing");
			return;
		}
		User user = getUserDAO().findById(id);
		if(null==user){
			recordError("userdetails.user.notfound");
		}
		setUser(user);
	}
    
	public ILink getChartLink(int width, int height){
		return getChartEngineService().getLink(false,
				new Object[]{	Chart.USER_TRANSACTIONS.value(),
								getUser().getId(),
								new Integer(width), new Integer(height)});
	}
	
	/**
	 * Set the {@link User}
	 * @param user user to set
	 */
	public abstract void setUser(User user);
	public abstract User getUser();
	
	/**
	 * Injected {@link User} service
	 * @return instance of UserDAO
	 */
	@InjectObject("service:bookie.model.UserDAO")
	public abstract UserDAO getUserDAO();
	
	/**
	 * Get injected {@link GroupDetails} page
	 */
    @InjectPage("GroupDetails")
    public abstract GroupDetails getGroupDetails();
    
	@InjectObject("service:bookie.framework.ChartEngine")
	public abstract IEngineService getChartEngineService();
}
